﻿using System.Net.Mail;
using System.Threading;

namespace iBeauty.App.Helper
{
    class ModulSendMail
    {
        Thread th;
        public bool SendMail(string from, string pass, string to, string cc, string bcc, string sub, string body)
        {

            XuLySendMail xl = new XuLySendMail(from, pass, to, cc, bcc, sub, body);

            th = new Thread(new ThreadStart(xl.objSendMail));

            th.Start();
            Thread.Sleep(100);
            th.Join();

            return XuLySendMail.flag;
        }
    }
    class XuLySendMail
    {
        private string from;
        private string pass;

        private string to;
        private string cc;
        private string bcc;
        private string sub;
        private string body;
        private bool kt = false;
        public static bool flag;
        public XuLySendMail()
        { }
        public XuLySendMail(string from, string pass, string to, string cc, string bcc, string sub, string body)
        {
            this.from = from;
            this.pass = pass;
            this.to = to;
            this.cc = cc;
            this.bcc = bcc;
            this.sub = sub;
            this.body = body;

        }

        public void objSendMail()
        {
            try
            {

                System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();//(txtAdd.Text, txtTo.Text);


                MyMailMessage.From = new MailAddress(from);
                MailMessage msg = new MailMessage();
                if (to.Trim() != string.Empty)
                {
                    string[] toAddress = to.Trim().Split(';');
                    for (int i = 0; i < toAddress.Length; i++)
                    {
                        MyMailMessage.To.Add(toAddress[i].ToString());
                    }
                }

                if (bcc.Trim() != string.Empty)
                {
                    string[] bccMail = bcc.Trim().Split(';');
                    for (int i = 0; i < bccMail.Length; i++)
                    {
                        MyMailMessage.Bcc.Add(bccMail[i].ToString());
                    }
                }
                if (cc.Trim() != string.Empty)
                {
                    string[] ccMail = cc.Trim().Split(';');
                    for (int i = 0; i < ccMail.Length; i++)
                    {
                        MyMailMessage.CC.Add(ccMail[i].ToString());
                    }
                }
                MyMailMessage.Subject = sub;
                MyMailMessage.Body = body;
                MyMailMessage.IsBodyHtml = false;

                System.Net.NetworkCredential mailAuthentication = new
                System.Net.NetworkCredential(from, pass);

                System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
                mailClient.EnableSsl = true;
                mailClient.UseDefaultCredentials = false;
                mailClient.Credentials = mailAuthentication;
                mailClient.Send(MyMailMessage);


                kt = true;
            }
            catch
            {
                kt = false;
                //  flag = false;
            }
            //ck();
            flag = kt;
            //   MessageBox.Show("cuoi:"+ flag.ToString());//false
        }
    }
}
